<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\Permissions;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\model\Cpzx as CpzxModel;
use app\admin\model\Nav as NavModel;
class Cpzx extends Permissions
{
    public function index()
    {
        //实例化类
        $model = new cpzxModel();
        //接收index传进来的值
        $post = $this->request->param();
        //查询
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['title'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if (isset($post['nid']) and $post['nid'] > 0) {
            $where['nid'] = $post['nid'];
        }

        if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0')) {
            $where['status'] = $post['status'];
        }

        if (isset($post['is_top']) and ($post['is_top'] == 1 or $post['is_top'] === '0')) {
            $where['is_top'] = $post['is_top'];
        }

        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['create_time'] = [['>=',$min_time],['<=',$max_time]];
        }

        //三目运算符，判断是否有查询条件，如果没有就查询所有数据，如果有就按照条件查询
        $articles = empty($where) ? $model->order('create_time desc')->select(): $model->where($where)->order('create_time desc')->select();
        //foieach表链接查询查询出来name ,表的外键链接主键显示nav里面的字段
        foreach ($articles as $key => $value){
            $articles[$key]['name']= Db::name('nav')->where('id',$value['nav_id'])->value('navname');
        }
        //映射所有数据
        $this->assign('articles',$articles);
        //查询nav数据
        $info['cate'] = Db::name('nav')->select();
        //查询admin数据
        $info['admin'] = Db::name('admin')->select();
        //映射数据，返回点击查询结果
        $this->assign('info',$info);
        //映射index页面
        return $this->fetch();
    }


    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        //实例化model,查询本表内容
        $model = new cpzxModel();
        //实例化navmodel,查询表链接的内容
        $cpzxModel = new NavModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['nav_id', 'require', '请选择分类'],
                    ['img', 'require', '请上传缩略图'],
                    ['url', 'require', '请上传缩略图'],
                    ['content', 'require', '文章内容不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $cpzx = $model->where('id',$id)->find();
                if(empty($cpzx)) {
                    return $this->error('id不正确');
                }
                if(false == $model->allowField(true)->save($post,['id'=>$id])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功','admin/cpzx/index');
                }
            } else {
                //非提交操作
                //查询本表传过来的id
                $cpzx = $model->where('id',$id)->find();
                //查询表链接的id 进行修改
                $cates = $cpzxModel->select();
                //加L
                $cates_all = $cpzxModel->menulist($cates);
                //映射值
                $this->assign('cates',$cates_all);
                if(!empty($cpzx)) {
                    $this->assign('cpzx',$cpzx);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['nav_id', 'require', '请选择分类'],
                    ['img', 'require', '请上传缩略图'],
                    ['url', 'require', '请上传缩略图'],
                    ['content', 'require', '文章内容不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //设置创建人
                $post['admin_id'] = Session::get('admin');

                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    addlog($model->id);//写入日志
                    return $this->success('添加成功','admin/cpzx/index');
                }
            } else {
                //非提交操作
                //查询表链接
                $cate = $cpzxModel->select();
                //加L
                $cates = $cpzxModel->menulist($cate);
                //映射
                $this->assign('cates',$cates);
                //映射publis页面
                return $this->fetch();
            }
        }

    }


    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('cpzx')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                return $this->success('删除成功','admin/cpzx/index');
            }
        }
    }


    public function is_top()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('cpzx')->where('id',$post['id'])->update(['is_top'=>$post['is_top']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/cpzx/index');
            }
        }
    }


    public function status()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('cpzx')->where('id',$post['id'])->update(['status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/cpzx/index');
            }
        }
    }
}
